<?php
namespace Client\Controller;
use Think\Controller;
class WoController extends BaseController{
//class WoController extends Controller{
	
	//========================
	protected function _initialize(){
		parent::_initialize();
		header("Content-Type: text/html;charset=utf-8");
		$this->assign('tj',time());
		
		//p($_SESSION);die;
		
		$this->assign('session',$_SESSION);
	}
	
	//我的首页
    public function index(){
    		//抵用券==============
    		$cardnum=M('bb_user_card')->where(array('openid'=>$_SESSION['user']['openid']))->count();
		$this->assign('cardnum',$cardnum);
		//=========
		$this->display();
    }
	//我的信息详情
	public function woinfo(){
		$this->display();
	}
	//修改昵称
	public function editusername(){
		
		$this->display();
	}
	//选择地区
	public function editarea(){
		$area=$this->area_data();
		$area=\bb\Data::channelLevel($area, 0, '-', 'area_id');
		
		$this->wxsdk();
		$this->assign('area',$area);
		$this->display();
	}
	//绑定手机
	public function editphone(){
		
		$this->display();
	}
	
	
	//钱包
	public function qianbao(){
		$this->display();
	}
	
	//电子卡
	public function card(){
		$this->display();
	}
	//电子详情
	public function cardinfo(){
		$this->display();
	}
	//电子充值
	public function cardrecharge(){
		$this->display();
	}
	
	//抵用卷
	public function volume(){
		//抵用券==============
    		$card=M('bb_user_card')->where(array('openid'=>$_SESSION['user']['openid']))->select();
		$this->assign('card',$card);
		$this->assign('newtime',strtotime(time()));
		//=========
		$this->display();
	}
	
	public function wo(){
		$this->display();
	}
	
	//我的推广2维码
	public function fenxiao(){
		
		$options['appid']=C('BB_APPID_client');
		$options['secret']=C('BB_SECRET_client');
		$wx=new \bb\Wechat($options);
		$re=$wx->getQRUrl_str($_SESSION['user']['id']);
		//var_dump($_SESSION['user']);die;
		//echo $_SESSION['user']['id'];die;
		$this->assign('tuigungimg',$re);
		
		//$map['path']=array('like',$_SESSION['user']['path'].$_SESSION['user']['id'].'%');
		//$xianxiannum=M('client_user')->where($map)->count();
		$m=M('client_user');
		$arr=array();
		$tmp=$m->field('id')->where(array('pid'=>$_SESSION['user']['id']))->order('addtime desc')->select();
		$total=$m->where(array('pid'=>$_SESSION['user']['id']))->count();
		
		if($tmp){
			foreach($tmp as $v){
				$arr[]=$v['id'];
			}
			$tmp2=$m->field('id')->where(array('pid'=>array('in',$arr)))->select();
			$total=$total + $m->where(array('pid'=>array('in',$arr)))->count();
			
			if($tmp2){
				$arr2=array();
				foreach($tmp2 as $v){
					$arr2[]=$v['id'];
				}
				
				$total=$total + $m->where(array('pid'=>array('in',$arr2)))->count();
			}
		}
		
		
		
		$this->assign('xianxiannum',$total);
		
		$this->display();
	}

	//我的返现
	public function fanxian(){
		$log = M('log_msg');
		
		$arr = $log->where('openid="'.$_SESSION['user']['openid'].'"')->select();
		
		$user =M('client_user');
		foreach($arr as $k=>$v){
			$username= $user->field('username')->where('openid="'.$arr[$k]['openid'].'"')->find();
			$arr[$k]['username'] =$username['username'];
			
		}
		
		$this->assign('arr',$arr);
		$this->display();
	}
	//我的下线会员
	public function xianxia_user(){
		$m=M('client_user');	
		$type=intval(I('type'))?intval(I('type')):1;
		if($type==1){
			$cache=$m->where(array('pid'=>$_SESSION['user']['id']))->order('addtime desc')->limit(50)->select();
			foreach ($cache as $k => $v){
				$wxuser=M('weixin_client_user')->field('headimgurl')->where(array('openid'=>$v['openid']))->find();
				$cache[$k]['headimgurl']=$wxuser['headimgurl'];
			}
			$total=$m->where(array('pid'=>$_SESSION['user']['id']))->count();
		}
		
		if($type==2){
			$arr=array();
			$tmp=$m->field('id')->where(array('pid'=>$_SESSION['user']['id']))->order('addtime desc')->select();
			if($tmp){
				foreach($tmp as $v){
					$arr[]=$v['id'];
				}
				$cache=$m->where(array('pid'=>array('in',$arr)))->limit(50)->select();
				$total=$m->where(array('pid'=>array('in',$arr)))->count();
			}else{
				$total=0;
			}
		}
		
		if($type==3){
			$arr=array();
			$tmp=$m->field('id')->where(array('pid'=>$_SESSION['user']['id']))->order('addtime desc')->select();
			if($tmp){
				foreach($tmp as $v){
					$arr[]=$v['id'];
				}
				$tmp2=$m->field('id')->where(array('pid'=>array('in',$arr)))->select();
				if($tmp2){
					$arr2=array();
					foreach($tmp2 as $v){
						$arr2[]=$v['id'];
					}
					$cache=$m->where(array('pid'=>array('in',$arr2)))->order('ctime desc')->limit(50)->select();
					$total=$m->where(array('pid'=>array('in',$arr2)))->count();
				}else{
					$total=0;
				}
			}else{
				$total=0;
			}
		}
		$this->assign('type',$type);
		$this->assign('total',$total);
		$this->assign('cache',$cache);
		$this->display();
	}
	//投诉建议
	public function toushujianyi(){
		
		$this->display();
	}
	
	public function dotoushujianyi(){
		$data['type'] = $_POST['type'];
		$data['msg'] = $_POST['content'];
		$data['openid']=$_SESSION['user']['openid'];
		$data['addtime']=time();
		$notice = M('notice');
		$notice->add($data);
		$this->display();
	}
	
	//余额充值
	public function czyue(){
		$user=M('client_user')->where(array('openid'=>$_SESSION['user']['openid']))->find();
		$this->assign('user',$user);
		$this->display();
	}
	
	//充值
	public function czpay(){
		$czmoeny=I('czmoney');
		if($czmoeny){
			$czlog['openid']=$_SESSION['user']['openid'];
			$czlog['czid']=time();
			$czlog['msg']='会员充值';
			$czlog['money']=$czmoeny;
			$czlog['addtime']=time();
			
			$re=M('usercz_log')->add($czlog);
			if($re){
				$this->wxpay($czmoeny,$czlog['czid'],$_SESSION['user']['openid']);
			}
		}
	}
	//=============================
	public function wxsdk(){
		//分享js 参数------------------------------
		$options['appid']=C('BB_APPID_client');
		$options['secret']=C('BB_SECRET_client');
		$wx=new \bb\Wechat($options);	//new 微信类
		$rejsapi=$wx->getjsapi();
		$this->assign('jsapi',$rejsapi);
		//-----------------------------------
	}
	public function area_data(){
		//缓存地区信息
		//S('privince',null);
		$authname='privince';
		if($rs = S($authname)){
			return $rs;
		}else{
			$area=M('bb_area')->select();
			$expire = 60*60*24;	//每天缓存一次
			S($authname,$area,$expire);
			return $area;
		}
	}
	//====================
	public function jifeninfo(){
		$m=M('log_msg');
		$jf=$m->where(array('openid'=>$_SESSION['user']['openid'],'type'=>1))->select();
		$this->assign('jf',$jf);
		$this->display();
	}
	
	public function yueinfo(){
		
		
	}
	
//==========================================================================
	//微信支付代码
	public function wxpay($money,$czid='',$openid=''){
		header("Content-Type: text/html;charset=utf-8");
		
		//$money=$money;
		$money=0.01;
		$options['appid']=C('BB_APPID_client');
		$options['secret']=C('BB_SECRET_client');
		$options['mch_id']=C('BB_MCHID_client');
		$options['payKey']=C('BB_MCHPAYKEY_client');
		$wx=new \bb\Wechat($options);
		
		$pay['openid']=$_SESSION['openid'];
		$pay['body']='余额充值';
		$pay['orderId']=time();
		$pay['money']=$money;
		$pay['attach']=$czid.'|'.$openid;
		$pay['notify_url']='http://wx.tingzan88.com/Client/notify/wx_cznotify';
		$data=$wx->unifiedOrder($pay['openid'],$pay['body'],$pay['orderId'],$pay['money'],$pay['notify_url'],$pay['attach']);
		foreach($data as $key => $v) {
			$str .= "{$key}={$v}&";
		}
		echo $url="http://wx.tingzan88.com/wxczpay.php?$str".'orderid='.$czid."&money=".$pay['money'];
		header("Location:".$url); 
	}
}